package com.freight.base.serviceimpl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.freight.base.dao.AreaDao;
import com.freight.base.entity.Area;
import com.freight.base.service.AreaService;
import com.freight.base.vo.AreaListVo;
import com.freight.base.vo.AreaVo;
import com.freight.core.common.vo.SearchVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.*;
import java.util.*;

/**
 * 区域维表接口实现
 *
 * @author huyingtai
 */
@Slf4j
@Service
@Transactional
public class AreaServiceImpl implements AreaService {

    @Autowired
    private AreaDao areaDao;

    @Override
    public AreaDao getRepository() {
        return areaDao;
    }

    @Override
    public List<AreaListVo> getByAreaList(String code) {
        return areaDao.getByAreaList(code);
    }

    @Override
    public Page<Area> findByCondition(Area area, SearchVo searchVo, Pageable pageable) {

        return areaDao.findAll(new Specification<Area>() {
            @Nullable
            @Override
            public Predicate toPredicate(Root<Area> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {

                // TODO 可添加你的其他搜索过滤条件 默认已有创建时间过滤
                Path<Date> createTimeField = root.get("createTime");
                // TODO 可添加你的其他搜索过滤条件 默认已有创建时间过滤
                Path<String> areaCode = root.get("areaCode");
                // TODO 可添加你的其他搜索过滤条件 默认已有创建时间过滤
                Path<String> areaName = root.get("areaName");
                // TODO 可添加你的其他搜索过滤条件 默认已有创建时间过滤
                Path<String> cityCode = root.get("cityCode");

                List<Predicate> list = new ArrayList<>();

                // 创建时间
                if (StrUtil.isNotBlank(searchVo.getStartDate()) && StrUtil.isNotBlank(searchVo.getEndDate())) {
                    Date start = DateUtil.parse(searchVo.getStartDate());
                    Date end = DateUtil.parse(searchVo.getEndDate());
                    list.add(cb.between(createTimeField, start, DateUtil.endOfDay(end)));
                }

                // 模糊搜素
                if (StrUtil.isNotBlank(area.getAreaCode())) {
                    list.add(cb.like(areaCode, '%' + area.getAreaCode() + '%'));
                }

                // 模糊搜素
                if (StrUtil.isNotBlank(area.getAreaName())) {
                    list.add(cb.like(areaName, '%' + area.getAreaName() + '%'));
                }

                // 模糊搜素
                if (StrUtil.isNotBlank(area.getCityCode())) {
                    list.add(cb.like(cityCode, '%' + area.getCityCode() + '%'));
                }

                Predicate[] arr = new Predicate[list.size()];
                cq.where(list.toArray(arr));
                return null;
            }
        }, pageable);
    }

    @Override
    public AreaVo getAreaVoLevel(String areaCode) {
        return areaDao.getAreaVoLevel(areaCode);
    }

    @Override
    public List<AreaVo> getAreaVoLevel(List<String> areaCodes) {
        return areaDao.getAreaVoLevel(areaCodes);
    }

    @Override
    public HashMap<String, AreaVo> getAreaVoLevelMap(List<String> areaCodes) {
        List<AreaVo> areaVos = areaDao.getAreaVoLevel(areaCodes);
        HashMap<String, AreaVo> areaMap = new HashMap<>();
        for (AreaVo areaVo : areaVos) {
            areaMap.put(areaVo.getAreaCode(), areaVo);
        }
        return areaMap;
    }

    @Override
    public HashMap<String, AreaVo> getAreaVoLevelMap(HashSet<String> areaCode) {
        List<String> areaCodes = new ArrayList<>();
        areaCodes.addAll(areaCode);
        List<AreaVo> areaVos = areaDao.getAreaVoLevel(areaCodes);
        HashMap<String, AreaVo> areaMap = new HashMap<>();
        for (AreaVo areaVo : areaVos) {
            areaMap.put(areaVo.getAreaCode(), areaVo);
        }
        return areaMap;
    }

}